{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9449d607",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2 as cv\n",
    "\n",
    "H,S,V = 0,0,0\n",
    "s = 0\n",
    "#变量作用域\n",
    "def callback_H(x):\n",
    "    global H\n",
    "    H = x\n",
    "def callback_S(x):\n",
    "    global S\n",
    "    S = x\n",
    "def callback_V(x):\n",
    "    global V\n",
    "    V = x\n",
    "def callback_s(x):\n",
    "    global s\n",
    "    s = x\n",
    "#create a black image ,a window\n",
    "img = np.zeros((300,512,3),np.uint8)\n",
    "winName = 'image'\n",
    "cv.namedWindow(winName)\n",
    "\n",
    "#create trackbars for color change\n",
    "tbName = 'H'\n",
    "tbInitValue = 90\n",
    "tbMaxValue = 180\n",
    "cv.createTrackbar(tbName,winName,tbInitValue,tbMaxValue,callback_H)\n",
    "cv.createTrackbar('S',winName,255,255,callback_S)\n",
    "cv.createTrackbar('V',winName,255,255,callback_V)\n",
    "\n",
    "#create switch for ON/OFF functionality\n",
    "switch = 'OFF/ON'\n",
    "cv.createTrackbar(switch,winName,0,1,callback_s)\n",
    "\n",
    "while(1):\n",
    "    cv.imshow('image',img)\n",
    "    k = cv.waitKey(1) & 0xFF\n",
    "    if k == 27:\n",
    "        break\n",
    "    if s ==0:\n",
    "        img[:] = 0\n",
    "    else:\n",
    "        img[:] = [H,S,V]\n",
    "        img[:] = cv.cvtColor(img,cv.COLOR_HSV2BGR)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff23a6db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
